//
//  Strategy.m
//  HYHOME
//
//  Created by cjy on 16/3/2.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "Strategy.h"
#import "ButtonTableViewCell.h"
#import "PushStrategy.h"
#import "ModelS.h"
#import "MagazineTableViewCell.h"
#import "PushWeb.h"
#import "NetworkingHandle.h"
#import "DKNightVersion.h"
@interface Strategy ()<UITableViewDataSource,UITableViewDelegate,ButtonTableViewCellDelegate>

@property (nonatomic,retain)UITableView *tableView;
@property (nonatomic,retain)NSMutableArray *arr;

@end

@implementation Strategy



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    self.title = @"装修攻略";
    [self createtableView];
    
    [self handle];
    
}


- (void)createtableView{


    self.tableView  = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
    
    [self.view addSubview:self.tableView];
    
    
    [self.tableView release];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    

    [self.tableView registerClass:[ButtonTableViewCell class] forCellReuseIdentifier:@"pool1"];
    
    [self.tableView registerClass:[MagazineTableViewCell class] forCellReuseIdentifier:@"pool"];
    self.tableView.dk_backgroundColorPicker =  DKColorWithRGB(0xffffff, 0x343434);
//    self.tableView.dk_separatorColorPicker = DKColorWithRGB(0xaaaaaa, 0x313131);
    
    
}

- (void)handle{

    self.arr = [NSMutableArray array];
    NSString *str = @"http://api.bohejiaju.com/v1/channels/16/items?limit=20&offset=0";
    [NetworkingHandle networkingWithURL:str completeHandel:^(NSURLResponse *response, NSData *data, id result) {
       
        NSDictionary *dicdata=[result objectForKey:@"data"];
        NSArray *arrI = [dicdata objectForKey:@"items"];
        for (NSDictionary *dica in arrI) {
            
            ModelS *model = [[ModelS alloc]init];
            model.coverUrl = [dica objectForKey:@"cover_image_url"];
            model.title = [dica objectForKey:@"title"];
            
            model.content_url = [dica objectForKey:@"content_url"];
            model.ida = [dica objectForKey:@"id"];
            [self.arr addObject:model];
            [model release];
        }
            
    
        
        [self.tableView reloadData];
    }];



}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


    return self.arr.count + 1;


}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0) {
        
        ButtonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
        
      
        cell.delegate = self;
        return cell;
        
        
    }else{
        
    MagazineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
        
        ModelS *model = [self.arr objectAtIndex:indexPath.row - 1];
        
        cell.dk_backgroundColorPicker =  DKColorWithRGB(0xffffff, 0x343434);
        cell.selectionStyle = UITableViewCellAccessoryNone;
        [cell passModel:model];
        
        return cell;
    }

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{


    if (indexPath.row == 0) {
        
        return 180;
    }else{
    
    
        return 205;
    
    }
    
    

}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    PushWeb *url = [[PushWeb alloc]init];
    
    ModelS *model = [self.arr objectAtIndex:indexPath.row  -1];
   
    [url passModel:model];
    
    
    [self.navigationController pushViewController:url animated:YES];

}


- (void)didClickItem:(ModelS *)model{

    PushStrategy *push = [[PushStrategy alloc]init];
    
    push.article_id = [model.article_id integerValue];

    [self.navigationController pushViewController:push                                animated:YES];

    [push release];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
